
define([
    "jquery",
    "IO",
    "bootbox"
], function ($, IO, bootbox) {

    $(function () {

        var year_1=1;
        var year_2=2;
        var year_3=3;

        var activity_mid=localStorage.getItem("activity_mid");

        gettable(year_1,activity_mid);

        var act_id;
        //编辑
        $(document).on("click",".editaa",function(){
            act_id=$(this).attr("act_id");
            var act_name=$(this).attr("act_name");
            $(".group_name").val(act_name);

        });

        $("#btnUpdate").click(function(){
            var mid=act_id;
            var research_money=$('.research_money').val();
            var research_type=$("#selectType option:selected").val();
            var research_reslut=$('.research_reslut').val();
            var year_id = $("#year_id_hidden").val();
            var params={};
            params.mid=mid;
            params.research_money=research_money;
            params.research_type=research_type;
            params.research_reslut=research_reslut;
            params.activity_mid=activity_mid;
            if(params.research_reslut==""){
                alert("请输入研发结果");
                return;
            }
            if(params.research_type==""){
                alert("请输入研发类型");
                return;
            }
            if(params.research_money==""){
                alert("请输入研发金额");
                return;
            }
            if(isNaN(research_reslut)&&isNaN(params.research_money)){
                 alert("请输入数字");
                return;
            }

            IO.post("/shapan/api/ResearchesEdit",params,function(d){
                if(d.code=="0"){
                    $('#myModal').modal('hide');
                    $('.research_money').val("");
                    $('.research_reslut').val("");
                    gettable(year_id,activity_mid);
                }
                else {
                    $('#myModal').modal('hide');
                    $('.research_money').val("");
                    $('.research_reslut').val("");
                    alert("操作失败");
                }
            })
        });

        $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
            // 获取已激活的标签页的名称
            var activeTab = $(e.target).text();
            // 获取前一个激活的标签页的名称
            if(activeTab=="第一年度"){
                $("#year_id_hidden").val("1");
                gettable(1,activity_mid);
            }
            else if(activeTab=="第二年度"){
                $("#year_id_hidden").val("2");

                gettable(2,activity_mid);
            }
            else
            {
                $("#year_id_hidden").val("3");
                gettable(3,activity_mid);
            }

        });

    });

    function gettable(year_id,activity_mid){

        var grid;
        //每列数据列性
        var columns = [
            {
                title: '公司名',
                dataIndex: 'group_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].group_name + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '研发投入(万元)',
                dataIndex: 'research_money',
                dataSource: function (data, datarow, gridobj, current_column) {

                    if(data[datarow].research_money==null){
                        return "<span>"+""+"</span>";
                    }
                    else {
                        return "<span>" +data[datarow].research_money + "</span>";
                    }

                }
            },
            {
                title: '研发状态',
                dataIndex: 'research_reslut',
                dataSource: function (data, datarow, gridobj, current_column) {

                    if(data[datarow].research_reslut==null){
                        return "<span>"+""+"</span>";
                    }
                    else if(data[datarow].research_reslut==0){
                        return "<img src='/images/buy.png'/>"+"<span>&nbsp;" +"购买" + "</span>";
                    }
                    else  if(data[datarow].research_reslut>0){
                        return "<img src='/images/done.png'/>"+"<span>&nbsp;" +"成功" + "</span>";
                    }
                    else {
                        return "<img src='/images/not.png'/>"+"<span>&nbsp;" +"失败" + "</span>";
                    }
                }
            },
            {
                title: '研发排名',
                dataIndex: 'research_reslut',
                dataSource: function (data, datarow, gridobj, current_column) {
                    if(data[datarow].research_reslut==null){
                        return "<span>"+""+"</span>";
                    }
                    else {
                        return "<span>" +data[datarow].research_reslut + "</span>";
                    }
                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<img src='/images/edit.png'/>&nbsp;"+
                        "<span class='editaa' act_name='"+data[datarow].group_name+"' act_id='"+data[datarow].mid+"' data-toggle='modal' data-target='#myModal' style='color: #5186b2; font-size: 16px;'>编辑</span>";
                }
            }
        ];

        //数组初始化

        function dataInit(year_id,activity_mid) {

            grid = new Grid("/shapan/api/ResearchesShow", $("#table_my_toast"+year_id), $("#paging_my_toast"+year_id), 1, 10, columns, get_input_param(year_id,activity_mid), 10);

            //初始化
            grid.Init();
        }

        function get_input_param(year_id,activity_mid) {

            var param="year_id="+year_id+"&activity_mid="+activity_mid;
            return param;
        }

        dataInit(year_id,activity_mid);
    }

});